from django_redis import get_redis_connection
from .models import OrderModel,QuestionModel,QImageModel
from pdata.models import PListModel
from pdata.serial import PListSerializer
def get_pay_order(user_id):
    redis_conn = get_redis_connection("order")
    cursor = '0'
    while cursor != 0:
        cursor, keys = redis_conn.scan(cursor=cursor, match=f"*{user_id}*")
        for key in keys:
            pay_status = redis_conn.hget(key, 'pay_status').decode()
            if pay_status == "0" or pay_status == "1":
                order_id = key.decode().split(':')[-1]
                patient_id = redis_conn.hget(key, 'patient_id').decode()
                order = OrderModel.objects.get(order_id=order_id)
                question = QuestionModel.objects.filter(order=order).first()
                images_list = []
                for i in QImageModel.objects.filter(question=question):
                    images_list.append(i.get_image_path)
                patient = PListModel.objects.get(id=patient_id)
                patient_serial = PListSerializer(patient)
                data = {
                    "pay_status":order.pay_status,
                    "order_id": order.order_id,
                    "pay_price": order.pay_price,
                    "content": question.write,
                    "images_list": list(images_list),
                    'order': order.id,
                    "create_time": order.create_time.strftime("%Y-%m-%d %H:%M"),
                }
                data.update(patient_serial.data)
                return data












